<?php

namespace common\model;

use think\Hook;
use think\model\RelationModel;

class TemplateTodoModel extends RelationModel
{

    //自动验证
    protected $_validate = [
    ];

    //自动完成
    protected $_auto = [
        ['created', 'time', self::MODEL_INSERT, 'function'],
        ['updated', 'time', self::MODEL_INSERT, 'function'],
        ['created_by', 'fill_created_by', self::MODEL_INSERT, 'function'],
        ['updated_by', 'fill_created_by', self::MODEL_UPDATE, 'function'],
        ['tenant_id', 'auto_fill_tenant_id', self::MODEL_INSERT, 'function'],
        ['start_man', 'json_encode', self::EXISTS_VALIDATE, 'function'],
        ['participant_man', 'json_encode', self::EXISTS_VALIDATE, 'function'],
        ['job_option', 'json_encode', self::EXISTS_VALIDATE, 'function'],
        ['after_option', 'json_encode', self::EXISTS_VALIDATE, 'function'],
        ['tenant_id', 'auto_fill_tenant_id', self::MODEL_INSERT, 'function'],
    ];

    /**
     * @param $value
     * @return array|mixed
     */
    public function getStartManAttr($value)
    {
        if (!empty($value)) {
            return json_decode($value, true);
        }
        return [];
    }

    /**
     * @param $value
     * @return array|mixed
     */
    public function getParticipantManAttr($value)
    {
        if (!empty($value)) {
            return json_decode($value, true);
        }
        return [];
    }

    /**
     * @param $value
     * @return array|mixed
     */
    public function getJobOptionAttr($value)
    {
        if (!empty($value)) {
            return json_decode($value, true);
        }
        return [];
    }

    /**
     * @param $value
     * @return array|mixed
     */
    public function getAfterOptionAttr($value)
    {
        if (!empty($value)) {
            return json_decode($value, true);
        }
        return [];
    }

    /**
     * @param $value
     * @return array|mixed
     */
    public function getUpstreamDownstreamTemplateAttr($value)
    {
        if (!empty($value)) {
            return json_decode($value, true);
        }
        return [];
    }
    /**
     * @param $result
     * @param $pkName
     * @param $data
     * @param $options
     * @param $writeEvent
     * @return void
     */
    public function _after_update($result, $pkName, $data, $options, $writeEvent)
    {
        parent::_after_update($result, $pkName, $data, $options, $writeEvent);
        if(!empty($options['disable_db_after']) && $options['disable_db_after'] === true){
            // 禁用db后处理事件
            return;
        }


        // 旧数据
        $oldStartMan = json_encode(json_decode($this->oldUpdateData['start_man'] ?? '{}', true), JSON_UNESCAPED_UNICODE);
        $oldParticipantMan = json_encode(json_decode($this->oldUpdateData['participant_man'] ?? '{}', true), JSON_UNESCAPED_UNICODE);
        $oldJobOption = json_encode(json_decode($this->oldUpdateData['job_option'] ?? '{}', true), JSON_UNESCAPED_UNICODE);
        $oldAfterOption = json_encode(json_decode($this->oldUpdateData['after_option'] ?? '{}', true), JSON_UNESCAPED_UNICODE);
        // 新数据
        $newStartMan = json_encode(json_decode($this->newUpdateData['start_man'] ?? '{}', true), JSON_UNESCAPED_UNICODE);
        $newParticipantMan = json_encode(json_decode($this->newUpdateData['participant_man'] ?? '{}', true), JSON_UNESCAPED_UNICODE);
        $newJobOption = json_encode(json_decode($this->newUpdateData['job_option'] ?? '{}', true), JSON_UNESCAPED_UNICODE);
        $newAfterOption = json_encode(json_decode($this->newUpdateData['after_option'] ?? '{}', true), JSON_UNESCAPED_UNICODE);
        // 新旧数据比对是否有更改
        if ($result) {
            $params['template_todo_id'] = $data['id'];
            if (md5($oldStartMan) != md5($newStartMan)) {
                $params['start_man_is_update'] = 'yes';
                Hook::listen('after_template_todo_update', $params);
            }
            if (md5($oldParticipantMan) != md5($newParticipantMan)) {
                $params['participant_man_is_update'] = 'yes';
                Hook::listen('after_template_todo_update', $params);
            }
            if (md5($oldJobOption) != md5($newJobOption)) {
                $params['job_option_is_update'] = 'yes';
                Hook::listen('after_template_todo_update', $params);
            }
            if (md5($oldAfterOption) != md5($newAfterOption)) {
                $params['after_option_is_update'] = 'yes';
                Hook::listen('after_template_todo_update', $params);
            }
        }
    }

}
